/**
 * @author shwj 20180419
 */
import { getCampusFrame } from 'api/eschool';
import Cookies from 'js-cookie';

const page = {
    state: {
        phoneData: [], // 组件数据集合
        selectItem: [], // 选中对对应的数据
        selectIndex: -1 // 选中的索引
    },

    mutations: {
        ADD_COMPONENT(state, data){ // 添加组件到PHONE中显示
            state.phoneData.push(data);
        },
        SET_PANELDATA(state, data) { // 设置每个组件添加不同数据
            if(data.type=='carousel') {
                state.phoneData[data.index].carouselImg.push(data.carouselImgData);
            }
            if(data.type=='menus') {
                state.phoneData[data.index].menusList.push(data.menuList);
            }
            if(data.type=='text') {
                state.phoneData[data.index].content = data.content;
                state.phoneData[data.index].align = data.align;
                state.phoneData[data.index].textFontSize = data.textFontSize;
                state.phoneData[data.index].color = data.color;
            }
            if(data.type=='pic') {
                state.phoneData[data.index].content = data.pic;
            }
        },
        SELECT_ITEM(state, index) { // 选中对应的组件
            state.selectIndex = index;
            state.selectItem = state.phoneData[index];
        },
        REMOVE_COMPONENT(state, index) { // 删除选中的组件
            state.phoneData.splice(index, 1);
            state.selectIndex = -1;
        },
        SET_PHONEDATA(state, arr) { // 设置phoneData
            // arr.forEach((item)=>{
            //     state.phoneData.push(item);
            // });
            state.phoneData = arr;
        },
        RESET_SELECT_INDEX(state) { // 取消选中
            state.selectIndex = -1;
        },
        UPLOAD_SET_IMG(state, obj) { // 更新轮播对对应的图片
            state.phoneData[state.selectIndex].carouselImg[obj.innerSelectIndex].pic = obj.pic;
        },
        SET_LASTSELECT(state) {
            state.selectIndex = state.phoneData.length - 1;
        }
    },
    actions: {
        SWAP_ITEMS({ commit, state }, data) {
            let curArray = data.phoneData;
            let $index1 = data.curIndex;
            let $index2 = data.nextIndex;
            curArray[$index1] = curArray.splice($index2, 1, curArray[$index1])[0];
            commit('SET_PHONEDATA', curArray);
            state.selectIndex = $index2;
        },
        // 初始化组件数据
        INIT_PHONEDATA({ commit, state }, schoolId) {
            return new Promise((resolve, reject)=>{
                getCampusFrame(schoolId).then(res => {
                    if (res && res.data.content) {
                        // this.shareLogoPic = res.data.content.pic;
                        // this.schoolMotto = res.data.content.schoolMotto;
                        if (res.data.content) {
                            resolve(res.data.content);
                        }
                        // this.SET_PHONEDATA(JSON.parse(res.data.content.html) || []);
                        Cookies.set('frameId', res.data.content.frameId);
                        Cookies.set('shareId', res.data.content.shareId);
                        commit('SET_PHONEDATA', JSON.parse(res.data.content.html) || []);
                    }
                }).catch((error) => {
                    reject(error)
                });
            })
        }
    }
}

export default page;